<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 2017/6/21
 * Time: 10:40
 */
namespace Biz;
use Think\Exception;

class Article extends \Base\BaseBiz{

    private $article = '';
    private $article_content = array();


    public function setArticle(\Orm\Article\ArticleClass $article) {
        $this->article = $article;
    }
    public function setArticleContent($article_content) {
        $this->article_content[] = $article_content;
    }

    /**
     * 创建文章
     * @throws \Exception
     * @var \Orm\ArticleContent\ArticleContentClass $content
     * @var \Orm\Article\ArticleClass $this->article
     */
    public function create(){
        $this->article->beginTransaction();
        try {
            $this->article->insert();
            foreach ($this->article_content as $content) {
                $content['article_id'] = $this->article['id'];
                $content->insert();
            }
        } catch (Exception $e) {
            $this->article->rollBack();
            throw new \Exception($e->getMessage());
        }
        $this->article->commit();
    }
}